package observer00;

/**
 * 间谍来啦
 */
public class Spy extends Thread{
    private HanFeiZi hanFeiZi;
    private LiSi liSi;
    private String type;

    //通过构造函数来表明，我要监控的是谁，谁来监控，要监控什么
    public Spy(HanFeiZi hanFeiZi, LiSi liSi, String type) {
        this.hanFeiZi = hanFeiZi;
        this.liSi = liSi;
        this.type = type;
    }

    @Override
    public void run() {
        while (true){
            //判断被观察者在做什么
            if(this.type.equals("breakfast")){
                //System.out.println(Thread.currentThread().getName() + "llll");
                if(this.hanFeiZi.isHavingBreakfast()){
                    this.liSi.update("在吃饭");
                    //被观察者状态重置
                    this.hanFeiZi.setHavingBreakfast(false);
                }
            }else{
                if(this.hanFeiZi.isHavingFun()){
                //    韩非子在玩
                    this.liSi.update("在娱乐");
                    this.hanFeiZi.setHavingFun(false);
                }
            }
        }
    }
}
